# -*- coding:utf-8 -*-
import os,sys
import re
import traceback
import time
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), os.pardir))
import supeanut_config
from CommonLib.mylog import mylog
from CommonLib.StockTool import StockTool
from CommonLib.FileTool.CsvTool import CsvTool
from DataAPI.ExRightsAPI import ExRightsAPI


'''
作者：supeanut
创建时间：2016-xx-xx xx:xx:xx
功能描述：
	xxx
	xxxxx
相关配置：
	supeanut_config.XXX
历史改动：
	2016-xx-xx: xxxxxx
'''
class ExRightSupport:
	# init what
	def __init__(self):
		#self.param = supeanut_config.PARAM
		self.stocktool = StockTool()
		self.exRights_api = ExRightsAPI()
		self.store_base_path = supeanut_config.EXRIGHTS_DATA_CSV_PATH
		self.csv_tool = CsvTool()

	def get_store_data(self, ):
		log = mylog('ExRightSupport.get_store_data', None)
		flag, stocks = self.stocktool.get_all_stock_code('all')
		if flag is False:
			return False, stocks
		unsuc_stocks = []
		for stock in stocks:
			time.sleep(10)
			flag, msg = self.exRights_api.get_exRights("sina", stock)
			if flag is False:
				log.warning(msg)
				unsuc_stocks.append(stock)
				continue
			# chuquan_date 的index 为0
			flag, msg = self.csv_tool.update_csvfile([0], msg, self.store_base_path, stock)
			if flag is False:
				log.warning(msg)
				unsuc_stocks.append(stock)
				continue
		if len(unsuc_stocks) == 0:
			return True, 'suc'
		else:
			return False, 'suc with waarning stocks,see detail log. warning stocks:%s'%(','.join(unsuc_stocks))


if __name__ == '__main__':
	obj = ExRightSupport()
	flag, msg = obj.get_store_data()
